
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;

int insertIntoSortedArray(int myArray[], int numEntries, int newValue);

int main (int argc, char **argv) {

  ifstream myfile (argv[1]);
  if (myfile.is_open())
  {
   int arrSize = 0;
    int arr[100];

    while ( true)
    {
        int x;
        myfile >> x;
        if (myfile.eof())
            break;
        arrSize = insertIntoSortedArray(arr, arrSize, x);
    }

    myfile.close();

  }

  else cout << "Failed to open the file.";

  return 0;
}

int insertIntoSortedArray(int myArray[], int numEntries, int newValue){
    myArray[numEntries] = newValue;

    numEntries += 1;
    int temp;

    for(int i = 0; i < numEntries - 1; i++)  {
      for(int j = i + 1; j < numEntries; j++)  {
         if(myArray[i] > myArray[j] ) {
            temp = myArray[i];
            myArray[i] = myArray[j];
            myArray[j] = temp;
         }
      }
   }

    for (int i = 0; i < numEntries; ++i){
        cout << myArray[i];
        if(i < numEntries-1)
            cout << ", ";
    }
    cout << endl;

    return numEntries;
}
